package com.roadjava.rbac.service.impl;

import com.roadjava.rbac.bean.entity.HistoryDO;
import com.roadjava.rbac.bean.entity.HistoryEQ;
import com.roadjava.rbac.bean.entity.LikedDO;
import com.roadjava.rbac.bean.entity.LikedEQ;
import com.roadjava.rbac.config.RedisCache;
import com.roadjava.rbac.mapper.HistoryMapper;
import com.roadjava.rbac.mapper.LikedMapper;
import com.roadjava.rbac.service.HistoryService;
import com.roadjava.rbac.service.LikedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author 29718
 */
@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;
    @Autowired
    private RedisCache redisCache;

    private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    String time = formatCurrentDateTime();

    @Override
    public void addHistory(HistoryEQ history) {
        history.setCreatedTime(time);
        historyMapper.addHistory(history);
    }

    @Override
    public void removeHistory(Integer resourceId, Long userId,Integer historiesStatus) {
        historyMapper.removeHistory(resourceId,userId,historiesStatus);
    }


    @Override
    public List<HistoryDO> getUserHistory(Long userId) {
        return historyMapper.getUserHistories(userId);
    }

//
//    @Override
//    public Long getHistoryount(Long resourceId) {
//        return historyMapper.getHistoryCount(resourceId);
//    }

    @Override
    public boolean checkUserHistory(Long userId, Long resourceId) {
        return historyMapper.existsByUserIdAndResourceId(userId, resourceId);
    }

    @Override
    public Boolean checkHistoryExists(Integer resourceId, Long userId) {
        return historyMapper.existsByResourceIdAndUserId(resourceId, userId);
    }

    private String formatCurrentDateTime() {
        try {
            // 格式化当前时间
            return LocalDateTime.now().format(formatter);
        } catch (DateTimeParseException e) {
            // 返回一个安全的默认时间字符串，或根据需要处理
            return "未知时间";
        }
    }
}